import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectedObj: {}, // 筛选条件
    pageNum: 1,
    longitude: '',
    latitude: '',
    originList: [],
    total: 0,
    type: '',
    sortList: [],
    activeIndex: 0,
    sortColumn: '',
    sortRule: '',
    showSelect: false,
    nameType: '1',
    tabList: ['绿色', '蓝色', '黄色', '红色', '黑色'],
    hasSelect: '绿色'
  },

  selectTab (e) {
    this.setData({
      hasSelect: e.detail.title,
      originList: [],
      pageNum: 1,
    })
    this.getInfos();
  },
  // 排序
  goSort () {
    this.setData({
      showSelect: true
    })
  },
  onClose () {
    this.setData({
      showSelect: false,
    });
  },
  selectOne (e) {
    const index = e.currentTarget.dataset.index
    const sortColumn = e.currentTarget.dataset.sortcolumn
    const sortRule = e.currentTarget.dataset.sortrule
    this.setData({
      activeIndex: index,
      sortColumn: sortColumn,
      sortRule: sortRule,
      showSelect: false,
    })
    this.clickCy();
  },
  clickCy (e) {
    // 切换后重置分页参数和列表原始数组
    this.setData({
      pageNum: 1,
      originList: [],
    });
    this.getInfos();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let list = []
    list = [
      {
        sortColumn: 'creditScore',
        sortRule: 'desc',
        label: '评分从高到低'
      },
      {
        sortColumn: 'creditScore',
        sortRule: 'asc',
        label: '评分从低到高'
      },
      {
        sortColumn: 'esdate',
        sortRule: 'asc',
        label: '成立日期由远及近'
      },
      {
        sortColumn: 'esdate',
        sortRule: 'desc',
        label: '成立日期由近及远'
      },
    ];
    this.setData({
      sortColumn: 'creditScore',
      sortRule: 'desc',
      nameType: '1',
      sortList: list
    })
    this.getInfos()
  },


  async getInfos () {
    wx.showLoading({
      title: "加载中",
    });
    // 全量的条件
    const params = {
      pageBO: {
        pageSize: 25,
        pageNum: this.data.pageNum,
      },
      sortColumn: this.data.sortColumn,
      sortRule: this.data.sortRule,
      envLevel: this.data.hasSelect + '等级'
    }
    console.log({ ...params }, '颜色的params');
    const { list, total } = await getApp().globalData.request(
      "/map/lookAroundV2",
      { ...params },
      "POST"
    );
    wx.hideLoading();
    console.log(list, '颜色的list')
    this.setData({
      originList: this.data.originList.concat(list),
      total: total,
      loading: false
    });
  },

  toprodetail (e) {
    wx.navigateTo({
      url: `/pages/mapDetail/mapDetail?corpKey=${e.detail.corpKey}&latitude=${e.detail.latitude}&longitude=${e.detail.longitude}&distance=${''}`,
    });
  },

  // 坐标系转化
  transMarker (item) {
    const resae = gcoord.transform(
      [item[0], item[1]],
      gcoord.GCJ02, // 当前坐标系
      gcoord.BD09 // 目标坐标系
    );
    return resae;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    this.setData({
      pageNum: ++this.data.pageNum
    })
    this.getInfos()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
